using UnityEngine;

[RequireComponent(typeof(LineRenderer))]
public class LineView : MonoBehaviour
{
    public MapItem start;
    public MapItem end;
    private LineRenderer lineRenderer;

    private void Awake()
    {
        lineRenderer = GetComponent<LineRenderer>();
    }

    /// <summary>
    /// 初始化起点和终点
    /// </summary>
    public void Setup(MapItem startItem, MapItem endItem)
    {
        start = startItem;
        end = endItem;

        // 初始化线条位置
        if (lineRenderer != null && start != null && end != null)
        {
            lineRenderer.positionCount = 2;
            lineRenderer.SetPosition(0, start.transform.position);
            lineRenderer.SetPosition(1, end.transform.position);
        }
    }

    private void Update()
    {
        if (start != null && end != null && lineRenderer != null)
        {
            lineRenderer.SetPosition(0, start.transform.position);
            lineRenderer.SetPosition(1, end.transform.position);
        }
    }
}
